package com.evil.account.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.evil.account.pojo.entity.UserRole;
import com.evil.common.account.dto.FindByIdsAndEIdReqDTO;
import com.evil.common.account.dto.user.UserBaseRespDTO;
import com.evil.common.account.dto.user.UserEnterpriseBaseDTO;
import com.evil.common.account.dto.user.role.UserRoleBaseRespDTO;
import com.evil.common.core.util.StreamUtil;
import com.evil.common.db.mapper.BatchMapper;
import com.evil.common.family.dto.FindByIdsReqDTO;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author liyang
 * @date 2023-01-15 11:15
 */
public interface UserRoleMapper extends BaseMapper<UserRole>, BatchMapper<UserRole, Long> {

    int updateBatch(List<UserRole> list);

    int batchInsert(@Param("list") List<UserRole> list);

    default UserRole findById(Long id) {
        return this.selectById(id);
    }

    List<UserRole> findByIds(@Param("ids") Collection<Long> ids, @Param("filterDeleted") boolean filterDeleted);

    UserRole findNormalUserRole(@Param("userId") Long userId, @Param("roleId") Long roleId);

    List<UserRole> findByUserId(@Param("enterpriseId") Long enterpriseId,
                                @Param("userId") Long userId,
                                @Param("filterDeleted") boolean filterDeleted);

    List<UserRole> findByRoleId(@Param("roleId") Long roleId, @Param("filterDeleted") boolean filterDeleted);

    List<Long> findUserIdsByRoleId(@Param("roleId") Long roleId, @Param("filterDeleted") boolean filterDeleted);

    List<UserRole> findByRIdAndUIds(@Param("roleId") Long roleId,
                                    @Param("userIds") Collection<Long> userIds,
                                    @Param("filterDeleted") boolean filterDeleted);

    List<UserRole> findByUserIds(@Param("enterpriseId") Long enterpriseId,
                                 @Param("userIds") Collection<Long> userIds,
                                 @Param("filterDeleted") boolean filterDeleted);

    List<UserBaseRespDTO> findUserBasesByRoleIds(FindByIdsReqDTO findByIdsReqDTO);

    List<Long> findUserIdsByRoleIds(FindByIdsReqDTO findByIdsReqDTO);

    List<UserRoleBaseRespDTO> findByUserEnterprise(UserEnterpriseBaseDTO userEnterpriseDTO);

    List<UserRoleBaseRespDTO> findUserRoleBaseByUserIds(FindByIdsAndEIdReqDTO findByIdsAndEIdReqDTO);

    default Map<Long, List<UserRoleBaseRespDTO>> findGroupUserRoleBaseByUserIds(FindByIdsAndEIdReqDTO findByIdsAndEIdReqDTO) {
        return StreamUtil.groupK(this.findUserRoleBaseByUserIds(findByIdsAndEIdReqDTO), UserRoleBaseRespDTO::getUserId);
    }
}
